package com.kk.goGame.controller;


import com.alibaba.druid.support.json.JSONUtils;
import com.kk.goGame.entity.User;
import com.kk.goGame.service.IUserService;
import com.kk.goGame.service.impl.UserServiceImpl;
import com.kk.goGame.utils.CodeUtils;
import com.kk.goGame.utils.R;
import com.qf.framwork.annotation.RequestParams;
import com.qf.framwork.base.BaseServlet;

import javax.servlet.annotation.WebServlet;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author kk
 * @since 2023-02-14
 */
@WebServlet("/userServlet")
public class UserController extends BaseServlet {
    private IUserService userService = new UserServiceImpl();

    public R login(@RequestParams("username") String username, @RequestParams("password") String password) {
        User is_login = userService.login(username, password);
        if (is_login != null) {
            is_login.setPassword("");
            return R.ok(is_login);
        } else {
            System.out.println("数据为空");
        }
        return R.fail("用户名或者密码错误");
    }

    public R register(@RequestParams("username") String username,
                      @RequestParams("password") String password,
                      @RequestParams("nickname") String nickname,
                      @RequestParams("sex") Integer sex,
                      @RequestParams("email") String email,
                      @RequestParams("phone") String phone,
                      @RequestParams("header") String header,
                      @RequestParams("score") Integer score) {
        User user = new User()
                .setUsername(username)
                .setPassword(password)
                .setNickname(nickname)
                .setSex(sex)
                .setHeader(header)
                .setEmail(email)
                .setPhone(phone)
                .setScore(score)
                .setCreateTime(LocalDateTime.now());
        //调用业务成保存对象
        System.out.println(user);
        Integer i = userService.register(user);
        if (i > 0) {
            return R.ok();
        }
        return R.fail("操作失败", i);
    }

    public R sendCode(@RequestParams("username") String username, @RequestParams("findtype") String findtype) {
        System.out.println("username："+username + ",选择的类型是：" + findtype);
        User user = userService.sedCode(username, findtype);
        if (user == null) {
            return R.fail("用户不存在");
        }

        String msg = "验证码已经发送至您";
        if (findtype.equals("1")) {
            String email = user.getEmail();
            String emailHeader = email.substring(0, 3);
            String emialFooter = email.substring(email.indexOf("@"));
            String msg1 = emailHeader + "****" + emialFooter;
            msg += msg1;
        } else if (findtype.equals("2")) {
            msg += user.getPhone().replace(user.getPhone().substring(3, 7), "****");
        }
        return R.ok(msg);
    }

    public R updatePassword(@RequestParams("username") String username,
                            @RequestParams("newPassword") String newPassword,
                            @RequestParams("verificationCode") Integer verificationCode) {
        try {
            if (verificationCode.equals(CodeUtils.getCode(username))) {
                int i = userService.updatePassword(newPassword, username);
                System.out.println("影响的行数："+i);
                if (i > 0) return R.ok();
            }
            return R.fail("验证码错误！");
        } finally {
            CodeUtils.remove(username);
        }
    }

    public R getScore(@RequestParams("uid") String uid){
        User user = userService.checkColumn("id", uid);
        return R.ok(null,user);
    }

    public R tongjiSex(){
        Map<String,Integer> sexMap= userService.tongjiSex();
        String jsonString = JSONUtils.toJSONString(sexMap);
        return R.ok(null,jsonString);
    }

    public R tongjiScore(){
        return R.ok(null,userService.tongjiScore());
    }
}
